/*************************************************************
   Copyright(C) 2017 by WuIslet
   All rights reserved.
   
   FindInPrefab.cs
   UnityFramework
   
   Created by WuIslet on 2018-02-01.
   
*************************************************************/
#if !USE_ILRUNTIME

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

public partial class FindInPrefab
{
    private void DrawOtherTool()
    {
        EditorGUILayout.BeginVertical("Box");
        FindPathWithGUID();
        EditorGUILayout.EndVertical();
        GUILayout.Space(20);
        EditorGUILayout.BeginVertical("Box");
        ColorTextChange();
        EditorGUILayout.EndVertical();
        GUILayout.Space(20);
        EditorGUILayout.BeginVertical("Box");
        ChineseTextFormat();
        EditorGUILayout.EndVertical();
        GUILayout.Space(20);
        EditorGUILayout.BeginVertical("Box");
        ShowGameobjectData();
        EditorGUILayout.EndVertical();
        GUILayout.Space(20);
    }

    private string findguid = "";
    private string result = "";
    private void FindPathWithGUID()
    {
        GUILayout.Label("GUID：");
        findguid = EditorGUILayout.TextField(findguid);
        if (GUILayout.Button("查找GUID"))
        {
            result = "";
            if (!string.IsNullOrEmpty(findguid))
            {
                result = AssetDatabase.GUIDToAssetPath(findguid);
            }
        }
        //显示查找结果
        if (string.IsNullOrEmpty(result))
        {
            EditorGUILayout.SelectableLabel("无数据");
        }
        else
        {
            EditorGUILayout.SelectableLabel(result);
        }
    }

    private bool is255 = true;
    private string colorHexText = "ffffffff";
    private void ColorTextChange()
    {
        is255 = EditorGUILayout.Toggle("255进制：", is255);
        if (is255)
            GUILayout.Label("ffffffff -> (255, 255, 255, 255)");
        else
            GUILayout.Label("ffffffff -> (1, 1, 1, 1)");
        colorHexText = EditorGUILayout.TextField(colorHexText);
        var result = "";
        if (colorHexText.Length == 6 || colorHexText.Length == 8)
        {
            int chunkSize = 2;
            int stringLength = colorHexText.Length;
            bool success = true;
            result = "(";
            for (int i = 0; i < stringLength; i += chunkSize)
            {
                if (i + chunkSize > stringLength) chunkSize = stringLength - i;
                var sub = colorHexText.Substring(i, chunkSize);
                try
                {
                    int num = Convert.ToInt32(sub, 16);
                    if (num > 255 || num < 0)
                    {
                        result = "错误";
                        success = false;
                        break;
                    }
                    else
                    {
                        result += string.Format("{0}, ", is255 ? num : num / 255f);
                    }
                }
                catch (Exception e)
                {
                    result = "错误";
                    success = false;
                }
            }

            if (success)
            {
                result = result.Remove(result.Length - 2);
                result += ")";
            }
        }
        EditorToolsUtils.DrawClickCopyLabel("结果", result, parent: window);
    }

    private string unicodeChinese = "";
    private void ChineseTextFormat()
    {
        unicodeChinese = EditorGUILayout.TextField(unicodeChinese);
        var chinese = Regex.Unescape(unicodeChinese);
        if (!string.IsNullOrEmpty(chinese))
            EditorGUILayout.LabelField(chinese);
    }

    private void ShowGameobjectData()
    {
        var select = Selection.activeGameObject;
        GUILayout.Label("选中物体：" + select);
        if (select != null)
        {
            GUILayout.Label("Hash：" + select.GetHashCode());
            GUILayout.Label("Path：" + AssetDatabase.GetAssetPath(select));

            if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(select, out var guid11, out long file11))
            {
                GUILayout.Label(" success:  " + guid11 + " , " + file11);
            }
            else
            {
                GUILayout.Label(" fail:  ");
            }

            GUILayout.Label("Icon: " + select.tag);

            var ttt = typeof(EditorGUIUtility);
            var mreal = ttt.GetMethod("GetIconForObject", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
            GUILayout.Label("  mreal>>>  " + mreal.Name);
            var rrr = mreal.Invoke(select, new object[] { select });
            var t2d = rrr as Texture2D;
            if (t2d == null)
            {
                GUILayout.Label("  rrr>>>  " + "null");
            }
            else
            {
                GUILayout.Label("  rrr>>>  " + t2d.name);
            }

            GUILayout.Label("  tag>>>  " + select.tag);
            if (GUILayout.Button("SetTag"))
            {
                select.tag = "UI_Template_Button";
            }
        }
    }
}
#endif